Welcome back.
In this lecture we're going to be learning about functions.
Functions are powerful tools that allow us to execute the same code over and over again for multiple
places in our scripts.
A function itself is a block of code that accomplishes a specific task.
A function can be called from places within our scripts to perform the task it's designed to complete.
To call a function means we would like the function to execute the code inside of it.
A function can also have information passed to it, and the function can also return information back
to where it was called.
You'll be using functions all of the time in your scripts.
So let's go ahead and see how to create one.
So the first thing we need to do to create a function is type out the function keyword.
After we type out the function keyword, we need to create an identifier or name for this function.
That way it's easy for us to call it.
This is the exact same thing for creating an identifier for a variable.
So I'm going to call this function here get some.
And after we create the identifier we need to create a set of parentheses.
And inside of these parentheses is where we can define information that can be passed to this function.
This function in particular I want to have numbers passed to it.
And I want this function to calculate the sum of all those numbers and return that sum back to where
this function was called.
So for example, for now I can create something called a parameter inside of this function.
This first parameter I'm going to call number one.
And then to create another parameter I just put a comma.
And then I'm going to create a second parameter called number two.
These are called parameters because these are just acting as placeholders right.
These are placeholders for the actual values that are going to be passed to the function.
The actual values passed to the function are known as the arguments.
While these are known as the placeholder values or the parameters.
Afterwards, we create a new block of code.
Here after hitting enter.
As you can see by this end statement, and inside of here is where we can create all of the code that
this particular function is supposed to complete.
For this function, I would like to add number one and number two together, and then return that result
back to where this function is called.
So that's going to be really easy to do.
We can create a variable in here.
I'm going to call this variable sum.
And it's going to be equal to the number one passed to this function plus the number two passed to this
function.
And then once we get this sum we can return that value back to where this function was called by using
another keyword.
And that is the return keyword.
And this keyword allows us to exit out of this function and go back to where it was called.
And with that we can pass a value we would like to return as well.
And you can also return multiple values by placing a comma and typing out the other values that you
would like to return back to where this function was called.
Pretty simple.
So to call this function, we type out the name for it or the identifier.
In this case it is get some.
And then afterwards we have to pass our two parentheses.
And here we can see that we can pass two different parameters to our function number one and number
two.
So for example I'm going to pass the number five.
And then I can pass the number ten.
And this is us calling the function or basically telling the interpreter to have the main thread of
execution hop into this function.
It's going to take these values with it and it's going to go into our function.
Five is placed inside of this parameter number one and ten is placed in the second parameter number
two.
It executes this code here which is adding number one and number two together storing it in this variable.
And then it encounters the return keyword along with the value that we've just calculated.
And it hops back to where this function was called and returns it, which means we can store the return
of this function inside of a variable.
So I'm going to create another variable here.
I'm going to call it result.
And it's going to be equal to the return of this function.
And then we can go ahead and print this result inside of our console.
So if we go to our test tab and then hit run.
As you can see, we get our result of 15, which is ten plus five.
Very cool.
The awesome thing about functions is you can call them as many times as you'd like.
So after we calculate the sum here, maybe we want to calculate another sum so we can override the result
variable equal to get some.
And this time I'll pass some numbers like 20 and 30.
And then we can print the result back into the console.
We could also override result again and get the sum of maybe -20 and 500.
And then print that result out.
So there's a lot of awesome ways we can use functions and call them wherever inside of our scripts.
Instead of us having to copy and paste this code in multiple places, all we need to do is call this
singular function that is, storing the code responsible for calculating the sum of two numbers.
So if we go ahead and hit run, as you can see, we get our totals in the console 1550 and 480.
Awesome.
Now, just like variables, functions can also be created in a global or local scope.
So right here our function has been created in the global scope of the script.
If I would like to restrict this function to the local scope, then all I need to do is type the local
keyword before my function.
And now I have created it in the local scope.
Or basically it is only accessible after the function has been declared, because when I make my function
global, I'm actually able to access get some even before it gets created.
And this is only in special circumstances.
As an example, if I had another function in here, I'm just going to call this example.
Inside of this function I can actually use git sum even though git sum is created after this function
is.
And that's because git sum is in the global scope of the script.
However, if I restrict it using the local keyword, all of a sudden we have no idea what git sum is.
And that's because git sum is now only accessible after it has been created.
So that's a very important difference to note when you make a function global, and when you restrict
that function to a scope, most of the time you'll want to have your functions in a local scope, but
there will be instances where you might want to create a function globally within your scripts.
Now, another cool thing that we can do with functions is actually define a variable or variadic number
of parameters to be passed to the function.
What do I mean by this?
Well, let's say we want to get the sum of numbers right.
But we want to be able to pass as many numbers as we want to this function.
We don't want to be restricted to only two parameters.
Well, in order to support this, what we can do is we can replace the parameters up top here in our
function with three dots.
And these three dots now represent a variable number of arguments that can be passed to this function.
We have no idea how many numbers might be passed to this function.
So that's why we're using the three dots to represent a variadic number of arguments or parameters.
Now what we can do is we can create a table here.
And I'm just going to call this all numbers.
And we can store all of the numbers passed to this function by typing out the three dots and storing
it inside of our table.
So if we pass like let's say 30 different numbers to this function, they're now all going to be stored
inside of this table or this array.
And what's the great thing about arrays.
Well they're organized and we can loop through them.
And since we can loop through arrays that means we can grab every single number in the array, add them
up altogether and then return that sum.
So what we're going to do is we're going to loop through every single value inside of the all numbers
array.
So we use the Ipairs function and then pass out all numbers and then put the do keyword.
And now we get every single value or number inside of our tables.
You might recognize that I have named this with an underline.
And this simply means that I do not care about this variable here.
I can name it whatever I like, like I can name it hello high whatever.
Or I could keep it at.
I.
But since in this particular instance where I'm looping through this array, I do not care for the index.
I only want the values.
So I denote it with an underline to signify that I do not care about this value here.
This is just kind of my throw away variable.
But once we do this, I'm also going to create another variable and I'm going to call it sum.
We'll set it equal to zero.
And then inside of my for loop what I want to do is I want to set the value of sum equal to itself plus
this value we're grabbing inside of our array.
And another cool thing with Lua is there's actually a faster way to type this out.
Instead of saying sum is equal to sum plus v, we can use the plus equal operator which achieves the
exact same effect.
So we're taking sum and we're adding v to it plus equal.
Very nice.
After we have calculated the sum for all of the numbers that gets passed to this function, then we
can return it back to where this function was called.
So this time, instead of being restricted to only passing two numbers, I can now pass as many numbers
as I like.
I can pass 30, I could pass a thousand, I could pass -50, and so on 1.29.
And I could just keep passing as many numbers as I like, because they're all going to be stored inside
of this table.
And we're going to loop through that table and add up all the numbers together and return that back
to where this function was called.
So now if we hit run.
As you can see, I get my very nice total of 1,126.29.
It took all of those numbers, placed it inside of the table.
We looped through that table, added up all the numbers together, and then returned it back to where
that function was called.
This is the very nice power of functions.
We don't have to copy all this code in multiple different places.
It's only in one area of our script.
And to execute all of this code, all we need to do is use the identifier, call the function and pass
whatever data we would like to our function.
It's awesome.
Now another thing I would like to show you with functions are something called recursive functions.
A recursive function is a function that calls itself either directly or indirectly.
This can cause an infinite loop of the function calling itself over and over again.
Unless you provide an opportunity to break out of this loop by a condition that needs to be evaluated.
So basically, this is another fancy way of creating an infinite loop by having a function call itself
over and over and over again.
So to demonstrate this I'm going to create a new function.
And I'm just going to call this function recursion example.
And inside.
At the very end of this function, what I want to do is call recursion example again.
So the first time I call this function within my script, it is going to execute this function over
and over and over and over again for infinity.
Unless I provide an opportunity to break out of this recursive loop.
So to do that I'm going to create a variable just before my function.
I'm just going to call it I and set it equal to zero.
And each time this function gets executed, I want to update the value of I plus equal one.
And then we also need to add a yield in this function if we want it to execute forever.
Because just like a while loop, if we have a recursive function that executes for infinity and it does
not yield, then we're going to crash our game.
However, since I want this recursion example function to execute for a finite number of times, I do
not need to add a yield statement, but I'm just going to do it for this example.
So we're going to wait for one second in here.
And what I'm going to do, or the first thing I want to do in this function is I want to check if I
is greater than ten.
So if I becomes greater than ten, then we can use the return keyword to go ahead and exit out of this
function.
We can use the return keyword to prematurely break out of this function, even if we don't have any
values to return.
If we ever store any result from this function, it's simply going to store it as nil.
Because again, we're not returning any values here.
This just allows us to break out of this function early.
So each time through this loop, we're going to be adding one waiting one second calling itself again.
And it's going to keep checking until I becomes greater than ten.
Once it does, the loop breaks and the recursive function is broken.
So what I'm also going to do is print out I into the console.
And then we can go ahead and call our function, because this function isn't going to execute until
we called it simply, we are just instantiating the function here and we're instantiating the function
here.
But the code inside of it will not execute until we actually call it.
By using the identifiers and passing the parentheses and any other possible values we'd like to add
or send to this function.
So now we can hit run and inside of the console.
As you can see, every single second it is counting up and adding one to our I variable.
And eventually, once it goes beyond ten, there we go.
It has stopped.
It hit 11, realized that 11 is greater than ten, and now that loop or that recursive function loop
has broken.
Recursion is just another tool we have in programming that you likely won't use often, but it's good
to know that it's available for you if you ever need to use recursive functions.
All righty.
And that's really all there is to functions.
They are simple blocks of code that can take an input and give us an output.
Hope you're learning a lot so far and I'll see you in the next lecture.